{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3bcdb9fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "#引用包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats\n",
    "import pymysql\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "plt.rcParams['font.family']='SimHei'\n",
    "plt.rcParams['axes.unicode_minus']= False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f1ec13c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>converted</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>851104</td>\n",
       "      <td>2017-01-21 22:11:48.556739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>804228</td>\n",
       "      <td>2017-01-12 08:01:45.159739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>661590</td>\n",
       "      <td>2017-01-11 16:55:06.154213</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>853541</td>\n",
       "      <td>2017-01-08 18:28:03.143765</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>864975</td>\n",
       "      <td>2017-01-21 01:52:26.210827</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>1</td>\n",
       "      <td>2017-01-21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id                   timestamp      group landing_page  converted  \\\n",
       "0   851104  2017-01-21 22:11:48.556739    control     old_page          0   \n",
       "1   804228  2017-01-12 08:01:45.159739    control     old_page          0   \n",
       "2   661590  2017-01-11 16:55:06.154213  treatment     new_page          0   \n",
       "3   853541  2017-01-08 18:28:03.143765  treatment     new_page          0   \n",
       "4   864975  2017-01-21 01:52:26.210827    control     old_page          1   \n",
       "\n",
       "         date  \n",
       "0  2017-01-21  \n",
       "1  2017-01-12  \n",
       "2  2017-01-11  \n",
       "3  2017-01-08  \n",
       "4  2017-01-21  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('ab_data.csv')\n",
    "data['date'] = data.timestamp.str[:10]\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "731058a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha = 0.05\n",
    "beta = 0.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "335997bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6701.938803160933"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#样本量计算\n",
    "control_p = data.converted[(data.group=='control')&(data.landing_page=='old_page')].mean()\n",
    "control_p_1_p_0=control_p*(1-control_p)\n",
    "z1_a = stats.norm.isf(alpha,loc=0,scale=1)\n",
    "z1_b = stats.norm.isf(beta,loc=0,scale=1)\n",
    "#假定实验组比对照组点击率提升1%位提升\n",
    "treatment_p=control_p+0.01\n",
    "p_p0 = 0.01\n",
    "p_1_p = treatment_p* (1-treatment_p)\n",
    "n = control_p_1_p_0*((z1_a + z1_b * np.sqrt(p_1_p/control_p_1_p_0))/p_p0)**2\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ec80ee37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6448536269514729"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#需要样本量6702个，满足"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "80ed46e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#T检验\n",
    "df = data[data.date=='2017-01-03'].groupby(['group','landing_page'],as_index=False)['converted'].mean()\n",
    "statisitic_t = df.converted[2] - df.converted[1]\n",
    "n1 = data[data.date=='2017-01-03'].converted[(data.group=='control')&(data.landing_page=='old_page')].size\n",
    "n2 = data[data.date=='2017-01-03'].converted[(data.group=='treament')&(data.landing_page=='new_page')].size\n",
    "sigma = np.sqrt(df.converted[2]*(1-df.converted[2])/n2 + df.converted[1]*(1-df.converted[1])/n1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "08cb01e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "statistic_p = 1-stats.norm.cdf(statisitic_t,0,sigma)\n",
    "statistic_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fb250ad1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1303863045004612"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#显著性水平大于0.05，选用原假设，继续使用Abanner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "94c5ae58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.11338571609917422"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#封装\n",
    "def ABtest_p(df:pd.DataFrame,group_col:str = None,value_col:str = None,alpha:float = 0.05)\n",
    "    if not group_col:\n",
    "        group_col = df.columns[0]\n",
    "    if not group_col:\n",
    "        group_col = df.columns[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f996cdc5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4ccd453",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8c35ae6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
